<?php
    require_once("config.php");
    
    function print_pager($pages, $perpage) {
        echo "<div style='position:relative; float:left; font-size:12pt;'>Page: ";
        for ($x = 1; $x < $pages+1; $x++) {
            if ($x == $page+1) 
                echo "&nbsp;&nbsp;<b>$x</b>&nbsp;&nbsp;";
            else 
                echo "&nbsp;&nbsp;<a href='browse.php?page=$x&show=$perpage'>$x</a>&nbsp;&nbsp;";
        }
        ?>
        </div>
        <div style='float:right;'>
            Showing
            <select id='perpage'>
                <option value='25' <?=($perpage == 25 ? "selected" : "")?>>25</option>
                <option value='50' <?=($perpage == 50 ? "selected" : "")?>>50</option>
                <option value='100' <?=($perpage == 100 ? "selected" : "")?>>100</option>
            </select><input type='button' onClick='location.href="browse.php?page=<?=$page+1?>&show=" + $("perpage").value' value='Go' />
            listings per page
            <br /></div>
        <?php
    }
    $browsemenu = new Menu("Browse Listings");
    $browsemenu->AddItem(new MenuItem("Search Listings","search.php",AuthConstants::ALL_LOGGED_IN));
    
    MenuManager::AddMenu("Browse Listings",$browsemenu);
    Functions::PrintHeader("Browse Listings");
    
    $page = (isset($_GET['page']) ? $_GET['page'] : 1);
    $page--;
    
    $perpage = (isset($_GET['show']) ? $_GET['show'] : 25);
    // don't want anyone overloading the server
    if ($perpage > 100)
        $perpage = 25;
    
    $query = "SELECT `id` FROM `listings` WHERE listed=1 ORDER BY `availableon` DESC;";
    $count = DBM::CountRows($query);
    $rows = DBM::FetchAllRows($query);
    $show = array();
    
    $pages = ceil($count / $perpage);
    print_pager($pages,$perpage);
    for ($x = ($page * $perpage); $x < (($page * $perpage) + $perpage); $x++) {
        if (isset($rows[$x]))
            echo new ListingRow($rows[$x]['id']);
        else 
            break;
    }
    
    echo "<div style='margin-top:15px; border-top:solid 2px black;'>";
    print_pager($pages,$perpage);
    echo "</div>";
    Functions::PrintFooter();

?>
